package com.ru.application.creditscore.service;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import com.ru.application.creditscore.dao.ScoringSchemaDao;
import com.ru.application.creditscore.dao.support.SearchMode;
import com.ru.application.creditscore.dao.support.SearchParameters;
import com.ru.application.creditscore.domain.ScoringSchema;

public class ScoringSchemaServiceImpl implements ScoringSchemaService {

	private final static Logger logger = Logger.getLogger(ScoringSchemaServiceImpl.class);
	private ScoringSchemaDao scoringSchemaDao;

	public ScoringSchema findSchema(Integer id) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: Find schema with Id = " + id.toString());
		}
		ScoringSchema scoringSchema = new ScoringSchema();
		try {
			scoringSchema.setId(id);
			SearchParameters sp = new SearchParameters();
			sp.searchMode(SearchMode.EQUALS);
			scoringSchema = scoringSchemaDao.findUnique(scoringSchema, sp);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: Find schema OK with Id = " + id.toString());
		}
		return scoringSchema;
	}

	public List<ScoringSchema> findAll(SearchParameters sp) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: Find all schema");
		}
		List<ScoringSchema> listSchema = new ArrayList<ScoringSchema>();
		ScoringSchema scoringSchema = new ScoringSchema();
		try {
			listSchema = scoringSchemaDao.find(scoringSchema, sp);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: Find all schema OK");
		}
		return listSchema;
	}

	@Transactional
	public boolean updateSchema(ScoringSchema schema) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: Update schema with id = "
					+ schema.getId().toString());
		}
		boolean result = false;
		ScoringSchema scoringSchema = new ScoringSchema();
		try {
			scoringSchema = scoringSchemaDao.merge(schema);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled() && scoringSchema != null) {
			logger.debug("Schema: Update schema OK");
			result = true;
		}
		return result;
	}

	@Transactional
	public void createSchema(ScoringSchema schema) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: create schema");
		}

		try {
			scoringSchemaDao.save(schema);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: create schema OK");
		}
	}

	@Transactional
	public void removeSchema(Integer id) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: remove schema");
		}
		ScoringSchema scoringSchema = new ScoringSchema();
		scoringSchema.setId(id);
		try {
			scoringSchemaDao.delete(scoringSchema);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: remove schema OK");
		}
	}

	@Transactional
	public void updateListSchema(List<ScoringSchema> listSchema) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: updateListSchema.");
		}

		try {
			for (ScoringSchema schema : listSchema)
				scoringSchemaDao.merge(schema);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: updateListSchema OK");
		}
	}

	@Transactional
	public void createListSchema(List<ScoringSchema> listSchema) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: createListSchema.");
		}

		try {
			for (ScoringSchema schema : listSchema)
				scoringSchemaDao.save(schema);
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: createListSchema OK");
		}
	}

	@Transactional
	public void removeListSchema(List<Integer> listId) {
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: removeListSchema.");
		}

		try {
			for (Integer id : listId) {
				ScoringSchema schema = new ScoringSchema();
				schema.setId(id);
				scoringSchemaDao.delete(schema);
			}
		} catch (Exception e) {
			logger.error("Error: " + e.toString());
			throw new ServiceException(ServiceException.LIB_JPA_PB,
					ServiceException.CODE_JPA_PB, e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Schema: removeListSchema OK");
		}
	}

	public ScoringSchemaDao getScoringSchemaDao() {
		return scoringSchemaDao;
	}

	public void setScoringSchemaDao(ScoringSchemaDao scoringSchemaDao) {
		this.scoringSchemaDao = scoringSchemaDao;
	}
}
